/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.geometry;

import org.openaecfm.model.geometry.CartesianPoint;
import org.openaecfm.model.geometry.CartesianTransformationOperator;
import org.openaecfm.model.geometry.Direction;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class CartesianTransformationOperatorIO implements
		IEntityInstanceUtilInput<CartesianTransformationOperator>,
		IEntityInstanceUtilOutput<CartesianTransformationOperator> {
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void exportSPF(CartesianTransformationOperator pojo,
			EntityInstance ei, IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Axis1
		if (pojo.getAxis1() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getAxis1())));
		}

		// Set Axis2
		if (pojo.getAxis2() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getAxis2())));
		}

		// Set LocalOrigin
		EntityInstanceNameParameter local = new EntityInstanceNameParameter(
				appDataManager.getName(pojo.getLocalOrigin()));
		sei.addParameter(local);

		// Set Scale
		if (pojo.getScale() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new RealParameter(pojo.getScale()));
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveNotReferenceParameter(EntityInstance ei,
			CartesianTransformationOperator pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Scale
		Parameter scale = sei.getParameter(3);
		if (scale.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) scale;
			pojo.setScale(r.toDouble());
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			CartesianTransformationOperator pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Axis1
		Parameter axis1 = sei.getParameter(0);
		if (axis1.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) axis1;
			pojo.setAxis1((Direction) appDataManager.getObject(n
					.toInstanceName()));
		}

		// Set Axis2
		Parameter axis2 = sei.getParameter(1);
		if (axis2.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) axis2;
			pojo.setAxis2((Direction) appDataManager.getObject(n
					.toInstanceName()));
		}

		// Set LocalOrigin
		EntityInstanceNameParameter local = (EntityInstanceNameParameter) sei
				.getParameter(2);
		pojo.setLocalOrigin((CartesianPoint) appDataManager.getObject(local
				.toInstanceName()));
	}

}
